package com.limm.dao.upload;

import com.limm.util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;

public class UploadDao {

    private Connection conn = DbUtil.getConn();
    private PreparedStatement state = null;

    /**
     * 添加歌手
     *
     * @param name
     * @param photo
     */
    public void addSinger(String id, String name, String photo) {
        try {
            String sql = "INSERT INTO singer values(?,?,?)";
            state = conn.prepareStatement(sql);
            state.setString(1, id);
            state.setString(2, name);
            state.setString(3, photo);
            state.executeUpdate();
        } catch (Exception e) {
            System.out.println("歌手信息添加失败");
        } finally {
            close();
        }
    }

    /**
     * 添加专辑
     * @param id
     * @param specialName
     * @param singerId
     * @param photo
     */
    public void addSpecial(String id, String specialName, String singerId, String photo,String data) {
        try {
            String sql = "INSERT INTO special VALUES (?,?,?,?,?)";
            state = conn.prepareStatement(sql);
            state.setString(1, id);
            state.setString(2, specialName);
            state.setString(3, singerId);
            state.setString(4, photo);
            state.setString(5, data);
            state.executeUpdate();
        } catch (Exception e) {
            System.out.println("专辑插入失败");
        } finally {
            close();
        }
    }

    /**
     * 添加音乐
     * @param musicName
     * @param special
     * @param singer
     * @param src
     * @param uploader
     */
    public void addMusic(String id, String musicName, String special, String singer, String src, String uploader) {
        try {
            String sql = "INSERT INTO musicLibrary (m_id,m_name,m_special,m_singer,m_src,m_uploader) VALUES (?,?,?,?,?,?,?)";
            state = conn.prepareStatement(sql);
            state.setString(1, id);
            state.setString(2, musicName);
            state.setString(3, special);
            state.setString(4, singer);
            state.setString(5, src);
            state.setString(6, uploader);
            state.executeUpdate();
        } catch (Exception e) {
            System.out.println("上传失败");
        } finally {
            close();
        }
    }

    private void close() {
        try {
            if (state != null && conn != null) {
                state.close();
                conn.close();
            }
        } catch (Exception e) {

        }
    }
}
